﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrainManage.Server.Factory;
using GrainManage.Entities.Contact;
using GrainManage.Data.Util;

namespace GrainManage.Server.Business
{
    class ContactBusiness : BaseBusiness, GrainManage.Interface.IContact
    {

        //************************************************POST************************************************

        public OutputGetContactName GetContactName(InputGetContactName input)
        {
            var result = new OutputGetContactName();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.Contacts = service.GetContactName(input, input.Authentication.UserName);
                if (result.Contacts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetContactArea GetContactArea(InputGetContactArea input)
        {
            var result = new OutputGetContactArea();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.Areas = service.GetContactArea(input, input.Authentication.UserName);
                if (result.Areas.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetContactAddress GetContactAddress(InputGetContactAddress input)
        {
            var result = new OutputGetContactAddress();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.Addresses = service.GetContactAddress(input, input.Authentication.UserName);
                if (result.Addresses.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetContactsWithPhoto GetContactsWithPhoto(InputGetContactsWithPhoto input)
        {
            var result = new OutputGetContactsWithPhoto();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.Contacts = service.GetContactsWithPhoto(input.ContactFilter, input, result, input.Authentication.UserName);
                if (result.Contacts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetContacts GetContacts(InputGetContacts input)
        {
            var result = new OutputGetContacts();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.Contacts = service.GetContacts(input.ContactFilter, input, result, input.Authentication.UserName);
                if (result.Contacts.Count == 0)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputInsert Insert(InputInsert input)
        {
            var result = new OutputInsert();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.ContactID = service.Insert(input.Contact);
                if (result.ContactID > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.InsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputUpdate Update(InputUpdate input)
        {
            var result = new OutputUpdate();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                if (service.Update(input.Contact) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.UpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputDelete Delete(InputDelete input)
        {
            var result = new OutputDelete();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                if (service.Delete(input.ContactID) > 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.DeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchInsert BatchInsert(InputBatchInsert input)
        {
            var result = new OutputBatchInsert();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.ContactIDs = service.Insert(input.Contacts);
                if (!result.ContactIDs.Contains(0))
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchInsertFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchUpdate BatchUpdate(InputBatchUpdate input)
        {
            var result = new OutputBatchUpdate();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.InvalidContactIDs = service.Update(input.Contacts);
                if (result.InvalidContactIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchUpdateFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputBatchDelete BatchDelete(InputBatchDelete input)
        {
            var result = new OutputBatchDelete();
            try
            {
                var service = ObjectFactory.GetContactService(input);
                result.InvalidContactIDs = service.Delete(input.ContactIDs);
                if (result.InvalidContactIDs.Count == 0)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.BatchDeleteFailed, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }
    }
}
